
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from PySide6.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
    QPushButton, QFrame, QSizePolicy
)
from PySide6.QtGui import QFont, QIcon, QPixmap
from PySide6.QtCore import Qt, Signal, QSize, QTimer

class BreakDialog(QDialog):
    """休息提醒对话框"""
    
    # 信号定义
    breakStarted = Signal()  # 开始休息信号
    breakSkipped = Signal()  # 跳过休息信号
    
    def __init__(self, config_manager, parent=None):
        """初始化休息提醒对话框
        
        Args:
            config_manager: 配置管理器实例
            parent: 父窗口
        """
        super().__init__(parent)
        
        self.config_manager = config_manager
        
        # 设置窗口属性
        self.setWindowTitle("休息提醒")
        self.setWindowIcon(QIcon("app/resources/icons/bunny.png"))
        self.setMinimumSize(400, 300)
        self.setWindowFlags(
            Qt.WindowType.WindowStaysOnTopHint |  # 窗口置顶
            Qt.WindowType.CustomizeWindowHint |   # 自定义窗口
            Qt.WindowType.WindowTitleHint |       # 显示标题栏
            Qt.WindowType.Dialog                  # 对话框
        )
        
        # 创建UI组件
        self._create_ui()
        
        # 播放提醒声音
        if config_manager.get("general", "sound_enabled"):
            self._play_sound()
    
    def _create_ui(self):
        """创建UI组件"""
        # 创建主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(20, 20, 20, 20)
        main_layout.setSpacing(15)
        
        # 创建标题标签
        title_label = QLabel("休息啦！")
        title_label.setAlignment(Qt.AlignCenter)
        font = QFont()
        font.setPointSize(24)
        font.setBold(True)
        title_label.setFont(font)
        main_layout.addWidget(title_label)
        
        # 创建分隔线
        line = QFrame()
        line.setFrameShape(QFrame.HLine)
        line.setFrameShadow(QFrame.Sunken)
        main_layout.addWidget(line)
        
        # 创建提示标签
        tip_label = QLabel("工作告一段落，是时候让眼睛和身体休息一下了！")
        tip_label.setAlignment(Qt.AlignCenter)
        tip_label.setWordWrap(True)
        font = QFont()
        font.setPointSize(12)
        tip_label.setFont(font)
        main_layout.addWidget(tip_label)
        
        # 创建休息建议
        suggestion_label = QLabel(self._get_random_suggestion())
        suggestion_label.setAlignment(Qt.AlignCenter)
        suggestion_label.setWordWrap(True)
        font = QFont()
        font.setPointSize(10)
        suggestion_label.setFont(font)
        main_layout.addWidget(suggestion_label)
        
        # 创建按钮布局
        button_layout = QHBoxLayout()
        button_layout.setSpacing(10)
        
        # 创建开始休息按钮
        self.start_break_button = QPushButton("开始休息")
        self.start_break_button.setMinimumHeight(40)
        self.start_break_button.clicked.connect(self._on_start_break_clicked)
        button_layout.addWidget(self.start_break_button)
        
        # 创建跳过休息按钮
        self.skip_break_button = QPushButton("跳过休息")
        self.skip_break_button.setMinimumHeight(40)
        self.skip_break_button.clicked.connect(self._on_skip_break_clicked)
        button_layout.addWidget(self.skip_break_button)
        
        main_layout.addLayout(button_layout)
    
    def _get_random_suggestion(self):
        """获取随机休息建议"""
        suggestions = [
            "👁️ 眼睛放松：远眺窗外至少20秒，让眼睛聚焦远处。",
            "🧘 深呼吸：闭上眼睛，做5次深呼吸，感受身体的放松。",
            "💧 补充水分：喝一杯水，保持身体水分。",
            "🚶 站起来走动：简单走动几步，活动一下身体。",
            "🙆 伸展运动：伸展双臂，转动颈部，缓解肌肉紧张。",
            "👋 手腕放松：转动手腕，缓解打字带来的疲劳。",
            "🌿 环境转换：换个环境，让大脑得到短暂的休息。"
        ]
        
        import random
        return random.choice(suggestions)
    
    def _play_sound(self):
        """播放提醒声音"""
        # 这里可以使用QSound或其他方式播放声音
        # 为简化实现，这里暂不实现声音播放功能
        pass
    
    def _on_start_break_clicked(self):
        """开始休息按钮点击事件处理"""
        self.breakStarted.emit()
        self.accept()
    
    def _on_skip_break_clicked(self):
        """跳过休息按钮点击事件处理"""
        self.breakSkipped.emit()
        self.accept()
    
    def closeEvent(self, event):
        """窗口关闭事件处理
        
        Args:
            event: 关闭事件
        """
        # 阻止用户关闭窗口
        event.ignore()